package com.hk.servlet;

import com.hk.model.News;
import com.hk.model.NewsType;
import com.hk.service.INewsService;
import com.hk.service.INewsTypeService;
import com.hk.service.impl.NewsServiceImpl;
import com.hk.service.impl.NewsTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/17 10:20
 * State：Servlet 数据请求转发 控制层
 */
@WebServlet("/newsTrends")
public class NewsTrendsServlet extends HttpServlet {

    INewsTypeService iNewsTypeService = new NewsTypeServiceImpl();
    INewsService iNewsService = new NewsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        获取新闻类型数据
        List<NewsType> newsTypes = iNewsTypeService.findAllNewsType();
        //        获取新闻列表
        List<News> newsList = iNewsService.findAllNews();
//        存储数据
        req.setAttribute("newsTypes",newsTypes);
        req.setAttribute("newsList", newsList);
//        页面转发
        req.getRequestDispatcher("/jsp/news_and_trends.jsp").forward(req,resp);
//        页面重定向
//        resp.sendRedirect("/jsp/home.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
